/**
 * 
 */
package com.csair.youth.service.impl;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

import com.csair.youth.constant.Constant;
import com.csair.youth.dao.impl.hibernate3.SysConfigDaoImpl;
import com.csair.youth.database.SysConfig;

/**
 * @author mickey
 * 
 */
public class SysConfigServiceImpl {
	private SysConfigDaoImpl dao;

	public SysConfigDaoImpl getDao() {
		return dao;
	}

	public void setDao(SysConfigDaoImpl dao) {
		this.dao = dao;
	}

	public void saveSysConfig(SysConfig item) {
		if (item == null)
			return;
		if (StringUtils.isEmpty(item.getId()))
			item.setId(null);
		dao.saveOrUpdate(item);
	}

	public void deleteSysConfigById(String id) {
		if (!StringUtils.isEmpty(id))
			dao.deleteById(id);
	}

	public SysConfig getSysConfig(String id) {
		return dao.findById(id);
	}

	public void initGlobalSysConfigs(Map params) {
		// 资源库状态，全局下载速度等，有更多则加上即可
		// 首先看看数据库是否已经有
		if (getSysConfigByKey(Constant.REPO_STATUS_KEY) == null) {
			SysConfig statusConfig = new SysConfig();
			statusConfig.setName("资源库状态");
			statusConfig.setConfigKey(Constant.REPO_STATUS_KEY);
			statusConfig.setConfigValue(Constant.REPO_STATUS_OPEN);
			dao.save(statusConfig);
		}
		if (getSysConfigByKey(Constant.DOWNLOAD_SPEED_KEY) == null) {
			SysConfig downloadSpeedConfig = new SysConfig();
			downloadSpeedConfig.setName("客户端最大下载速度");
			downloadSpeedConfig.setConfigKey(Constant.DOWNLOAD_SPEED_KEY); // KB
			downloadSpeedConfig.setConfigValue(200 + "");
			dao.save(downloadSpeedConfig);
		}
		Properties prop = new Properties();
		try {
			prop.load(this.getClass().getClassLoader().getResourceAsStream(
					"properties/config_dev.properties"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SysConfig config = getSysConfigByKey(Constant.SERVER_IP_PORT_KEY);
		if (config == null) {
			SysConfig serverIpURLConfig = new SysConfig();
			serverIpURLConfig.setName("服务器IP+PORT+CONTEXT");
			serverIpURLConfig.setConfigKey(Constant.SERVER_IP_PORT_KEY);
			String ip = prop.getProperty("server.ip");
			String port = prop.getProperty("server.port");
			serverIpURLConfig.setConfigValue("http://" + ip
					+ (String) params.get("ContextPath"));
			if (!port.equals("80")) {
				serverIpURLConfig.setConfigValue("http://" + ip + ":" + port
						+ (String) params.get("ContextPath"));
			}
			dao.save(serverIpURLConfig);
		} else {
			String ip = prop.getProperty("server.ip");
			String port = prop.getProperty("server.port");
			String originalValue = config.getConfigValue();
			config.setConfigValue("http://" + ip
					+ (String) params.get("ContextPath"));
			if (!port.equals("80")) {
				config.setConfigValue("http://" + ip + ":" + port
						+ (String) params.get("ContextPath"));
			}
			if (!StringUtils.equals(originalValue, config.getConfigValue())) {
				dao.update(config);
			}
		}
		config = getSysConfigByKey(Constant.SERVER_FTP_KEY);
		if (config == null) {
			SysConfig ftpURLConfig = new SysConfig();
			ftpURLConfig.setName("服务器FTP");
			ftpURLConfig.setConfigKey(Constant.SERVER_FTP_KEY);
			String ip = prop.getProperty("server.ip");
			ftpURLConfig.setConfigValue("ftp://" + ip);
			dao.save(ftpURLConfig);
		} else {
			String ip = prop.getProperty("server.ip");
			String originalValue = config.getConfigValue();
			config.setConfigValue("ftp://" + ip);
			if (!StringUtils.equals(originalValue, config.getConfigValue())) {
				dao.update(config);
			}
		}
	}

	public SysConfig getSysConfigByKey(String key) {
		return dao.getSysConfigByKey(key);
	}
}
